package com.wsoft.sms.controller;

import com.wsoft.response.ResponseResult;
import com.wsoft.sms.query.template.*;
import com.wsoft.sms.service.ISmsTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 消息模版 前端控制器
 * @author zhanggukai
 * @since 2024年11月27日 14:31:52
 */
@RestController
@Tag(name = "消息模版管理")
@RequestMapping("/sms/template")
public class SmsTemplateController {

    @Resource
    private ISmsTemplateService templateService;
    @Operation(summary = "新增模版")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid TemplateInsertQuery query) {
        boolean b = templateService.insert(query);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }
    @Operation(summary = "修改模版")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid TemplateUpdateQuery query) {
        boolean b = templateService.update(query);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }
    @Operation(summary = "删除模版")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = templateService.delete(id);
        if (b){
            return ResponseResult.success("[sys.msg.success]");
        }
        else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }
    @Operation(summary = "获取模版详情")
    @GetMapping("/get")
    public ResponseResult getTemplate(@RequestParam("id") Long id) {
        return ResponseResult.success(templateService.getTemplate(id));
    }
    @Operation(summary = "获取模版列表")
    @PostMapping("/list")
    public ResponseResult listTemplate(@RequestBody @Valid TemplateListQuery query) {
        return ResponseResult.success((templateService.listTemplate(query)));
    }
    @Operation(summary = "测试发送")
    @PostMapping("/sendTest")
    public ResponseResult sendTest(@RequestBody @Valid TemplateTestQuery query) {
        templateService.sendTest(query);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "发送模版")
    @PostMapping("/sendTemplate")
    public ResponseResult sendTemplate(@RequestBody @Valid TemplateSendQuery query) {
        String content = templateService.sendTemplate(query.getTemplateCode(), query.getType(), query.getDataModelJson(), query.getTo());
        return ResponseResult.success(content);
    }
}
